import * as api from './api';
import {message} from 'antd';
import {formatMessage} from './intl';
export const ERROR_CODE = 'error';

export const GET_INIT_DATA_BEGIN = 'GET_INIT_DATA_BEGIN';
export const GET_INIT_DATA_SUCCESS = 'GET_INIT_DATA_SUCCESS';
export const GET_INIT_DATA_FAIL = 'GET_INIT_DATA_FAIL';

/**初始化 */
/*==============================查询列表================================= */
export const GET_LIST_DATA_BEGIN = 'GET_LIST_DATA_BEGIN';
export const GET_LIST_DATA_SUCCESS = 'GET_LIST_DATA_SUCCESS';
export const GET_LIST_DATA_FAIL = 'GET_LIST_DATA_FAIL';

const getListDataBegin = () => ({
    type: GET_LIST_DATA_BEGIN
});
const getListDataSuccess = data => ({
    type: GET_LIST_DATA_SUCCESS,
    data
});
const getListDataFail = () => ({
    type: GET_LIST_DATA_FAIL
});

export const SAVE_QUERY_DEALER = 'SAVE_QUERY_DEALER';
export const saveQueryDealer = data => ({
    type: SAVE_QUERY_DEALER,
    data
});


export const GET_LIST_COUNTRY_DATA_BEGIN = 'GET_LIST_COUNTRY_DATA_BEGIN';
export const GET_LIST_COUNTRY_DATA_SUCCESS = 'GET_LIST_COUNTRY_DATA_SUCCESS';
export const GET_LIST_COUNTRY_DATA_FAIL = 'GET_LIST_COUNTRY_DATA_FAIL';
export const GET_LIST_TYPE_SUCCESS = 'GET_LIST_TYPE_SUCCESS';
export const GET_LIST_TYPE_BEGIN = 'GET_LIST_TYPE_BEGIN';
export const GET_LIST_TYPE_FAIL = 'GET_LIST_TYPE_FAIL';
export const GET_LIST_RATE_SUCCESS = 'GET_LIST_RATE_SUCCESS';
export const GET_LIST_RATE_FAIL = 'GET_LIST_RATE_FAIL';
export const GET_LIST_RATE_BEGIN = 'GET_LIST_RATE_BEGIN';
export const getListCountryDataFail = () => ({
    type: GET_LIST_COUNTRY_DATA_FAIL
});
const getListTypeSuccess = data => ({
    type: GET_LIST_TYPE_SUCCESS,
    data
});
const getListTypeBegin = () => ({
    type: GET_LIST_TYPE_BEGIN
});
const getListRateBegin = () => ({
    type: GET_LIST_RATE_BEGIN
});
const getListTypeDataFail = () => ({
    type: GET_LIST_TYPE_FAIL
});
const getListRateSuccess = data => ({
    type: GET_LIST_RATE_SUCCESS,
    data
});
const getListRateDataFail = () => ({
    type: GET_LIST_RATE_FAIL
});
/*==============================查询action================================= */
//获取查询列表
export const getList = condition => dispatch => {
    const {dealerId} = condition;
    if(!dealerId)
        return message.warn(formatMessage({
            id: 'action.query.message1',
            defaultMessage: '总代不能为空'
        }));
    dispatch(getListDataBegin());
    dispatch(getListTypeBegin());
    dispatch(getListRateBegin());
    const list = api.getList(condition).then(res => {
        if(res.ok)
            dispatch(getListDataSuccess(res.data));
        else
            dispatch(getListDataFail());
    });
    const listType = api.getListType(condition).then(res => {
        if(res.ok)
            dispatch(getListTypeSuccess(res.data));
        else
            dispatch(getListTypeDataFail());
    });
    const listRate = api.getListRate(condition).then(res => {
        if(res.ok)
            dispatch(getListRateSuccess(res.data));
        else
            dispatch(getListRateDataFail());
    });
    return Promise.all([list, listType, listRate]).then(values => values[0] && values[1] && values[2]);
};

//修改查询条件
export const SAVE_QUERY_CONDITION = 'SAVE_QUERY_CONDITION';
export const saveQueryCondition = (name, value) => ({
    type: SAVE_QUERY_CONDITION,
    name,
    value
});
//重置查询条件
export const RESET_QUERY_PANEL = 'RESET_QUERY_PANEL';
export const resetQueryPanel = () => ({
    type: RESET_QUERY_PANEL,
});

export const RESET_QUERY_PANEL_DEALER = 'RESET_QUERY_PANEL_DEALER';
export const resetDealerQueryPanel = () => ({
    type: RESET_QUERY_PANEL_DEALER,
});

//查询月按钮搜索
export const querySearch = () => (dispatch, getState) => {
    const condition = getState().getIn(['page', 'appState', 'queryCondition']).toJS();
    dispatch(getList(condition));
};

